package org.jsola.hr.constant.convert;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.jsola.common.SafeKit;
import org.jsola.hr.constant.FileType;

/**
 *【档案类型】excel导入导出时字段值转换
 * @author JanusMix
 */
public class FileTypeConverter implements Converter<String> {

    @Override
    public Class<String> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        FileType fileType = FileType.findByDesc(cellData.getStringValue());
        if (fileType == null) {
            return SafeKit.getString(cellData.getStringValue());
        }
        return fileType.getValue();
    }

    @Override
    public CellData<String> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        FileType fileType = FileType.find(value);
        if (fileType == null) {
            return new CellData<>(value);
        }
        return new CellData<>(fileType.getDesc());
    }
}
